﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using Tenancy.Helper;

namespace Tenancy.Server
{
    /// <summary>
    /// Hbung_MapPlace 的摘要说明
    /// </summary>
    public class Hbung_MapPlace : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string Hbung_Address = context.Request.Params["Address"];
            if(string.IsNullOrEmpty(Hbung_Address))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("Address不能为空"));
                return;
            }
            string Hbung_City = context.Request.Params["City"];
            if (string.IsNullOrEmpty(Hbung_City))
            {
                context.Response.Write(GenerClass.SetParamErrorJsData("City不能为空"));
                return;
            }
            //string param = string.Format("ak=pcwUOIP2YAmGwSOjgwLDnLR35xPpaRxd&output=json&address={0}&city={1}", Hbung_Address, Hbung_City);
            //string outStr = GenerClass.HttpPost("http://api.map.baidu.com/geocoder/v2/", param);
            string param = string.Format("http://api.map.baidu.com/geocoder/v2/?ak=VDNGYTVD6n9vYRIlBQbHYjnYnqPBvbpf&output=json&address={0}&city={1}", Hbung_Address, Hbung_City);
      

            string outStr = GenerClass.HttpGet(param);//只能用get请求，不能使用post
            decimal JD = 0m;
            decimal WD = 0m;
            Model.Geocoder gModel = JsonHelper.JSONToObject<Model.Geocoder>(outStr);
            if (gModel != null)
            {
                if (gModel.Status == 0)
                {
                    JD = gModel.Result.Location.lng;
                    WD = gModel.Result.Location.lat;
                }
            }
            string strjson = "[{\"HbJD\":\"" + JD + "\",\"HbWD\":\"" + WD + "\"}]";
            context.Response.Write(GenerClass.SetSucessJsData("经纬度",strjson));

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}